//
//  MyPersonalInfoViewController.m
//  Toyohu
//
//  Created by Heige on 16/4/20.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "MyPersonalInfoViewController.h"
#import "BigTopImgTableViewCell.h"
#import "NormalTableViewCell.h"
#import "NotNormalTableViewCell.h"
#import "SpaceStyleOneTableViewCell.h"
#import "SpaceStyleTwoTableViewCell.h"
#import "SureButtonTableViewCell.h"
#import "IQActionSheetPickerView.h"

#import "LoginViewController.h"
#import "NameViewController.h"
#import "BabyNameViewController.h"
#import "GenderViewController.h"
#import "UIImagePickerController+ST.h"

#import "UIImagePickerController+ST.h"
#import "STPhotoKitController.h"
#import "LocalFileManager.h"
#import "MyFirstPageViewController.h"
#import "GPCustomMD.h"

#import "BeingLiftedView.h"
#import "RemoveBindingView.h"
#import "ReBindView.h"
#import "TieSuccessV.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "GPAFNetworking3.h"
#import "LoginManager.h"
#import "TYH_LoginLoginModel.h"

#import "SetSexView.h"


#import <QiniuSDK.h>
#import "GetUpload.h"



static NSString *BigTopImgTableViewCellReuseId = @"BigTopImgTableViewCell";
static NSString *NormalTableViewCellReuseId = @"NormalTableViewCell";
static NSString *NotNormalTableViewCellReuseId = @"NotNormalTableViewCell";
static NSString *SpaceStyleOneTableViewCellReuseId = @"SpaceStyleOneTableViewCell";
static NSString *SpaceStyleTwoTableViewCellReuseId = @"SpaceStyleTwoTableViewCell";
static NSString *SureButtonTableViewCellReuseId = @"SureButtonTableViewCell";

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height

typedef enum{
    CurrentImageCategoryAvatar = 0,
    CurrentImageCategoryCover
}CurrentImageCategory;


@interface MyPersonalInfoViewController ()<UITableViewDelegate,UITableViewDataSource,IQActionSheetPickerViewDelegate,STPhotoKitDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
{
    UITableView *myTableView;
    CurrentImageCategory _currentImage;
   
}

@end

@implementation MyPersonalInfoViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [myTableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    BigTopImgTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//    cell.backGroundView.image = [LocalFileManager getImageForKey:@"backGroundView"];
//    [cell.myUserImg setBackgroundImage:[LocalFileManager getImageForKey:@"myUserImg"] forState:UIControlStateNormal];
//    RemoveBindingView
//    BeingLiftedView
//    BeingLiftedView *rbView = [[BeingLiftedView alloc] initWithSuccessBlock:^(NSString *str) {
//        
//    } frame:self.view.frame];
//    
//    [self.view addSubview:rbView];

}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.showsVerticalScrollIndicator = NO;
    myTableView.showsHorizontalScrollIndicator = NO;
    myTableView.backgroundColor =[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    [self.view addSubview:myTableView];
    
    // 注册单元格
    [myTableView registerNib:[UINib nibWithNibName:@"BigTopImgTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:BigTopImgTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"NormalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NormalTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"NotNormalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NotNormalTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"SpaceStyleOneTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SpaceStyleOneTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"SpaceStyleTwoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SpaceStyleTwoTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"SureButtonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SureButtonTableViewCellReuseId];
    
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    [self setNotification];
}

#pragma mark --UITableViewDelegate  UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (void)setNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changePhone:) name:@"changePhone" object:nil];
}

- (void)changePhone:(NSNotification *)notification
{
    NormalTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    cell.content.text = notification.object;
    [self changInfo];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        myTableView.rowHeight=255;
        BigTopImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BigTopImgTableViewCellReuseId];
        if (!cell) {
            cell = (BigTopImgTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: BigTopImgTableViewCellReuseId];
        }
        [cell.returnButton addTarget:self action:@selector(goBackPreviousPage) forControlEvents:UIControlEventTouchDown];
        cell.myUserImg.layer.cornerRadius = cell.myUserImg.frame.size.height / 2;
        cell.myUserImg.clipsToBounds = YES;
        cell.myUserImg.layer.borderWidth = 1;
        cell.myUserImg.layer.borderColor = [UIColor whiteColor].CGColor;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell.myUserImg addTarget:self action:@selector(changeUserImg) forControlEvents:UIControlEventTouchDown];
        cell.backGroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeBackView)];
        [cell.backGroundView addGestureRecognizer:tap];
        [cell.myUserImg.imageView sd_setImageWithURL:[NSURL URLWithString:self.headUrlStr] placeholderImage:[UIImage imageNamed:@"图层-2"]];
        
        return cell;
    }
    else if (indexPath.row==1)
    {
        myTableView.rowHeight=48;
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NormalTableViewCellReuseId];
        if (!cell) {
            cell = (NormalTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: NormalTableViewCellReuseId];
        }
        cell.img.image = [UIImage imageNamed:@"user-1"];
        cell.name.text = @"个人昵称";
        cell.content.text = self.nameLabel;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==2)
    {
        myTableView.rowHeight=48;
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NormalTableViewCellReuseId];
        if (!cell) {
            cell = (NormalTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: NormalTableViewCellReuseId];
        }
        cell.name.text = @"手机号";
        cell.content.text = self.phoneStr;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==3)
    {
        myTableView.rowHeight=37;
        SpaceStyleOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SpaceStyleOneTableViewCellReuseId];
        if (!cell) {
            cell = (SpaceStyleOneTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SpaceStyleOneTableViewCellReuseId];
        }
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==4)
    {
        myTableView.rowHeight=48;
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NormalTableViewCellReuseId];
        if (!cell) {
            cell = (NormalTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: NormalTableViewCellReuseId];
        }
        cell.img.image = [UIImage imageNamed:@"baby-2"];
        cell.name.text = @"宝宝昵称";
        cell.content.text = self.childrenInfo.cName;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==5)
    {
        myTableView.rowHeight=48;
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NormalTableViewCellReuseId];
        if (!cell)
        {
            cell = (NormalTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: NormalTableViewCellReuseId];
        }
        cell.img.image = [UIImage imageNamed:@"age-3"];
        cell.name.text = @"出生年月";
        cell.content.text = [self.childrenInfo.cBirthday substringToIndex:10];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;
        
        
    }
    else
    {
        myTableView.rowHeight=48;
        NotNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NotNormalTableViewCellReuseId];
        if (!cell)
        {
            cell = (NotNormalTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: NotNormalTableViewCellReuseId];
        }
        if (self.childrenInfo.cSex == 1)
        {
            cell.img.image = [UIImage imageNamed:@"boy@2x"];
            cell.name.text = @"男";
        }
        else
        {
            cell.img.image = [UIImage imageNamed:@"girl@2x"];
            cell.name.text = @"女";
        }
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
}
//改变用户头像
-(void)changeUserImg
{
    _currentImage = CurrentImageCategoryAvatar;
    
    
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选取", nil];
    actionSheet.title = @"改变用户头像";
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}
//改变用户背景图
-(void)changeBackView
{
    _currentImage = CurrentImageCategoryCover;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选取", nil];
    actionSheet.title = @"改变封面";
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

#pragma mark - actionSheet

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    switch (_currentImage) {
        case CurrentImageCategoryAvatar:
        {
            if (buttonIndex == 0) {
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
                    pickerController.view.backgroundColor = [UIColor orangeColor];
                    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
                    pickerController.sourceType = sourceType;
                    pickerController.delegate = self;
                    pickerController.allowsEditing = YES;
                    
                    [pickerController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
                    [self presentViewController:pickerController animated:YES completion:nil];
                    
                    
                }else{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请允许本应用访问您的相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }

            }
            
            if (buttonIndex == 1) {
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
                    pickerController.view.backgroundColor = [UIColor orangeColor];
                    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
                    pickerController.sourceType = sourcheType;
                    pickerController.delegate = self;
                    pickerController.allowsEditing = YES;
                    
                    [pickerController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
                    [self presentViewController:pickerController animated:YES completion:nil];
                }else{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                                    message:@"请允许本应用访问您的相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
            }

        }
            break;
            
        case CurrentImageCategoryCover:
        {
            if (buttonIndex == 0) {
                UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
                
                if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
                    [controller setDelegate:self];
                    [self presentViewController:controller animated:YES completion:nil];
                }else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请允许本应用访问您的相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
            }
            
            if (buttonIndex == 1) {
                UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                [controller setDelegate:self];
                if ([controller isAvailablePhotoLibrary]) {
                    [self presentViewController:controller animated:YES completion:nil];
                }else{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                                    message:@"请允许本应用访问您的相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 1.STPhotoKitDelegate的委托

- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage
{
    
    BigTopImgTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.backGroundView.image = resultImage;
    
    [LocalFileManager saveImage:resultImage forKey:@"backGroundView"];
    if (self.changImageBlcok) {
        self.changImageBlcok();
    }
    [self changInfo];
}

- (void)setHeadImage:(UIImage *)image
{
    [SVProgressHUD showWithStatus:@"正在上传头像" maskType:SVProgressHUDMaskTypeGradient];
    [GPAFNetworking3 post:GetUploadToken parameters:@{@"fileName":@"headPortraitImage"} progress:nil success:^(id  _Nullable responseObject) {
        GetUploadGetUploadBase *base = [[GetUploadGetUploadBase alloc] initWithDictionary:responseObject];
        if (base.code != 1) {
            SHOW_ERROR
        } else {
            QNUploadManager *upManager = [[QNUploadManager alloc] init];
            NSData *data;
            if (!UIImagePNGRepresentation(image) ) {
                data = UIImageJPEGRepresentation(image, 1);
            } else {
                data = UIImagePNGRepresentation(image);
            }
            
            NSString *imageFile = [LocalFileManager getImageFile:@"temporaryMyUserImg"];
            
            QNUploadOption *qnOption = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
                if (percent >= 1) {
//                    [SVProgressHUD dismiss];
                } else {
//                    [SVProgressHUD showProgress:percent status:@"当前进度" maskType:SVProgressHUDMaskTypeGradient];
                 }
            }];
            
            [upManager putFile:imageFile key:base.msg.name token:base.msg.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                if (info.statusCode == 200) {
                    [GPAFNetworking3 post:SetHeadUrl parameters:@{@"pId":[[LoginManager shareLoginManager] getPid],@"url":[NSString stringWithFormat:@"%@%@",base.msg.domain,base.msg.name]} progress:nil success:^(id  _Nullable responseObject) {
                        TYH_LoginLoginModel *model = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
                        if (model.code != 1) {
                            SHOW_ERROR
                        }else{
                            [SVProgressHUD showSuccessWithStatus:@"修改成功" maskType:SVProgressHUDMaskTypeGradient];
                            [LocalFileManager saveImage:image forKey:@"myUserImg"];
                            BigTopImgTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
                            [cell.myUserImg setBackgroundImage:image forState:UIControlStateNormal];
                            [self changInfo];

                        }
                    } failure:^(NSString * _Nonnull errorLD) {
                        SHOW_NTERROR
                    }];
                }else{
                    [SVProgressHUD showErrorWithStatus:@"出错啦~\(≧▽≦)/~" maskType:SVProgressHUDMaskTypeGradient];
                }
            } option:qnOption];
        }
    } failure:^(NSString * _Nonnull errorLD) {
        SHOW_NTERROR
    }];
   
}

#pragma mark - 图形选取代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

    switch (_currentImage) {
        case CurrentImageCategoryAvatar:
        {
            [LocalFileManager saveImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forKey:@"temporaryMyUserImg"];
            [self dismissViewControllerAnimated:YES completion:nil];
            [self setHeadImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];
        }
            break;
        case CurrentImageCategoryCover:
        {
            [picker dismissViewControllerAnimated:YES completion:^{
                UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
                STPhotoKitController *photoVC = [STPhotoKitController new];
                photoVC.delegate = self;
                [photoVC setImageOriginal:imageOriginal];
                [photoVC setSizeClip:CGSizeMake(WIDTH, WIDTH/1.7)];
                [self presentViewController:photoVC animated:YES completion:nil];
            }];
        }
            break;
        default:
            break;
    }
    

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)goBackPreviousPage
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changInfo
{
    typedef void(^ChangeInfoBlock)(NSString *phoneText,NSString *babyNickName,NSString *dateOfBirthText,NSString *sexText);
    NormalTableViewCell *cell1 = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];//手机号
    NormalTableViewCell *cell2 = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];//昵称
    NormalTableViewCell *cell3 = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];//出生年月
    NotNormalTableViewCell * cell4 = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];//性别
    self.changeInfoBlock(cell1.content.text,cell2.content.text,cell3.content.text,cell4.name.text);
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==4)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"编辑宝宝昵称：" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        alert.tag = 101;
        [alert show];
        
    }
    else if (indexPath.row==5)
    {
        IQActionSheetPickerView *picker = [[IQActionSheetPickerView alloc] initWithTitle:@"出生年月" delegate:self];
        [picker setTag:6];
        [picker setActionSheetPickerStyle:IQActionSheetPickerStyleDatePicker];
        picker.maximumDate = [NSDate date];
        [picker show];
    }
     if (indexPath.row==2)
    {
//        user_phone
        if (GPGET(@"user_phone")) {
            [RemoveBindingView show];

        } else {
            //
            [ReBindView show:@""];
        }
    }
    if (indexPath.row==6) {
        
        NotNormalTableViewCell * cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];

        [SetSexView showWithSex:[cell.name.text isEqualToString:@"男"]?1:0 success:^(BOOL sex) {
            
            [SVProgressHUD showWithStatus:@"正在修改宝宝性别" maskType:SVProgressHUDMaskTypeGradient];
            WS
            [GPAFNetworking3 post:UpdateChildren parameters:@{@"info":[GPAFNetworking3 dictionaryToJson:@{@"pId":[[LoginManager shareLoginManager] getPid],@"children":@[@{@"cId":@(self.childrenInfo.cId),@"cName":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]]).content.text,@"cSex":sex?@(1):@(0),@"cBirthday":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]]).content.text}]}]} progress:nil success:^(id  _Nullable responseObject) {
                SS
                TYH_LoginLoginModel *model = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
                if (model.code != 1) {
                    SHOW_ERROR
                }else{
                    [SVProgressHUD showSuccessWithStatus:@"宝宝性别修改成功" maskType:SVProgressHUDMaskTypeGradient];
                    if (sex) {
                        cell.img.image = [UIImage imageNamed:@"boy@2x"];
                        cell.name.text = @"男";
                    }else{
                        cell.img.image = [UIImage imageNamed:@"girl@2x"];
                        cell.name.text = @"女";
                    }
                    [strongSelf changInfo];
                }
            } failure:^(NSString * _Nonnull errorLD) {
                SHOW_NTERROR
            }];

            
            
            
        }];
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101) {
        if (buttonIndex == 1) {
            
            if ([alertView textFieldAtIndex:0].text.length < 2) {
                [SVProgressHUD showErrorWithStatus:@"宝宝昵称必须在两位以上哦^.^" maskType:SVProgressHUDMaskTypeGradient];
                return;
            }
            
            
            SHOW_STATUS
            WS
            [GPAFNetworking3 post:UpdateChildren parameters:@{@"info":[GPAFNetworking3 dictionaryToJson:@{@"pId":[[LoginManager shareLoginManager] getPid],@"children":@[@{@"cId":@(self.childrenInfo.cId),@"cName":[alertView textFieldAtIndex:0].text,@"cSex":[((NotNormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]]).name.text isEqualToString:@"男"]?@(1):@(0),@"cBirthday":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]]).content.text}]}]} progress:nil success:^(id  _Nullable responseObject) {
                SS
                TYH_LoginLoginModel *model = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
                if (model.code != 1) {
                    SHOW_ERROR
                }else{
                    [SVProgressHUD showSuccessWithStatus:@"宝宝昵称修改成功" maskType:SVProgressHUDMaskTypeGradient];
                    UITextField *textField = [alertView textFieldAtIndex:0];
                    NormalTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
                    cell.content.text = textField.text;
                    [strongSelf changInfo];
                }
            } failure:^(NSString * _Nonnull errorLD) {
                SHOW_NTERROR
            }];
        }
        
    }else if (alertView.tag == 102) {
        
        
        
        SHOW_STATUS
        WS
        [GPAFNetworking3 post:UpdateChildren parameters:@{@"info":[GPAFNetworking3 dictionaryToJson:@{@"pId":[[LoginManager shareLoginManager] getPid],@"children":@[@{@"cId":@(self.childrenInfo.cId),@"cName":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]]).content.text,@"cSex":[((NotNormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]]).name.text isEqualToString:@"男"]?@(1):@(0),@"cBirthday":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]]).content.text}]}]} progress:nil success:^(id  _Nullable responseObject) {
            SS
            TYH_LoginLoginModel *model = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
            if (model.code != 1) {
                SHOW_ERROR
            }else{
                [SVProgressHUD showSuccessWithStatus:@"宝宝昵称修改成功" maskType:SVProgressHUDMaskTypeGradient];
                UITextField *textField = [alertView textFieldAtIndex:0];
                NormalTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
                cell.content.text = textField.text;
            }
        } failure:^(NSString * _Nonnull errorLD) {
            SHOW_NTERROR
        }];
        
        NotNormalTableViewCell * cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];
        if (buttonIndex == 0) {
                cell.img.image = [UIImage imageNamed:@"boy@2x"];
                cell.name.text = @"男";
        } else {
            cell.img.image = [UIImage imageNamed:@"girl@2x"];
            cell.name.text = @"女";
        }
        
    }
}

#pragma mark ---IQActionSheetPickerViewDelegate

-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *dataStr = (NSString *)[formatter stringFromDate:date];
    
    [SVProgressHUD showWithStatus:@"正在修改宝宝出生年月" maskType:SVProgressHUDMaskTypeGradient];
    WS
    [GPAFNetworking3 post:UpdateChildren parameters:@{@"info":[GPAFNetworking3 dictionaryToJson:@{@"pId":[[LoginManager shareLoginManager] getPid],@"children":@[@{@"cId":@(self.childrenInfo.cId),@"cName":((NormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]]).content.text,@"cSex":[((NotNormalTableViewCell *)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]]).name.text isEqualToString:@"男"]?@(1):@(0),@"cBirthday":dataStr}]}]} progress:nil success:^(id  _Nullable responseObject) {
        SS
        TYH_LoginLoginModel *model = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
        if (model.code != 1) {
            SHOW_ERROR
        }else{
            NormalTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
            cell.content.text = dataStr;
            [SVProgressHUD showSuccessWithStatus:@"宝宝出生年月修改成功" maskType:SVProgressHUDMaskTypeGradient];
            [strongSelf changInfo];
        }
    } failure:^(NSString * _Nonnull errorLD) {
        SHOW_NTERROR
    }];
    
   
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
